package cz.cacek.ebook;

import java.io.DataInput;
import java.io.DataOutput;

import javax.microedition.lcdui.Font;

/**
 *
 * 类说明： 此可
 *
 * @author QXO
 *
 */
public class SettingBean {

	private Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
			Font.SIZE_SMALL);

	private boolean fullscreen;
	private boolean displayStatus;

	private int colorScheme = 0;

	private boolean wrapSpaces=true;
	private boolean rotated;

	private boolean positionAsPerc = true;

	private BackLight backLight = new BackLight();

	private int bookIdx = Utils.NO_BOOK_SELECTED;

	private int scrollDelay = Utils.AUTOSCROLL_PAUSE;

	private SettingBean() {
		super();
	}

	/**
	 * 序列化方法，把该对象序列化为字节流（数组）以便于RMS存储、管理 Saves configuration to given DataOutput
	 *
	 * @param aDos
	 * @throws Exception
	 */
	public void saveSetting(DataOutput aDos) throws Exception {
		if(Utils.INFO){
			Utils.info(" saveSetting starting ...");
		}
		aDos.writeInt(this.getColorScheme());
		Font font = this.getFont();
		aDos.writeInt(font.getFace());
		aDos.writeInt(font.getStyle());
		aDos.writeInt(font.getSize());
		aDos.writeInt(this.getBookIdx());
		aDos.writeInt(this.getScrollDelay());

		BackLight bl = this.getBackLight();
		aDos.writeInt(bl.getBackLight());
		aDos.writeInt(bl.getAutoLight());
		aDos.writeBoolean(this.isWrapSpaces());
		aDos.writeBoolean(this.isFullscreen());
		aDos.writeBoolean(this.isRotated());
		aDos.writeBoolean(this.isDisplayStatus());
		aDos.writeBoolean(this.isPositionAsPerc());

		if(Utils.INFO){
			Utils.info(" saveSetting ok!");
		}
	}

	private static SettingBean instance;

	public static SettingBean getIntance() {
		if (null == instance) {
			instance = new SettingBean();
		}
		return instance;
	}

	/**
	 * //反序列化 Loads configuration from given DataInput
	 *
	 * @param aDis
	 * @throws Exception
	 */
	public void loadSetting(DataInput aDis) throws Exception {
		this.setColorScheme(aDis.readInt());
		int face = aDis.readInt();
		int style = aDis.readInt();
		int size = aDis.readInt();
		this.setBookIdx(aDis.readInt());
		this.setScrollDelay(aDis.readInt());

		BackLight tmpBL = new BackLight(aDis.readInt(), aDis.readInt());
		tmpBL.normal();
		this.setBackLight(tmpBL);

		this.setWrapSpaces(aDis.readBoolean());
		this.setFullscreen(aDis.readBoolean());
		this.setRotated(aDis.readBoolean());
		this.setDisplayStatus(aDis.readBoolean());
		this.setPositionAsPerc(aDis.readBoolean());
		// should be the last - because of redrawing :)
		this.setFont(Font.getFont(face, style, size));
	}

	public String toString() {
		final StringBuffer sb = new StringBuffer();
		sb.append("SettingBean").append(":")
		.append("\n").append("\t").append("colorScheme").append(":").append(colorScheme)
		.append("\n").append("\t").append("wrapSpaces").append(":").append(wrapSpaces)
		.append("\n").append("\t").append("rotated").append(":").append(rotated)
		.append("\n").append("\t").append("fullscreen").append(":").append(fullscreen)
		.append("\n").append("\t").append("displayStatus").append(":").append(displayStatus)
		.append("\n").append("\t").append("positionAsPerc").append(":").append(positionAsPerc)
		.append("\n").append("\t").append("scrollDelay").append(":").append(scrollDelay)
		.append("\n").append("\t").append("bookIdx").append(":").append(bookIdx)
		.append("\n").append("\t").append("font").append(":")
			.append("Font( face:").append(font.getFace()).append(",")
			.append("style:").append(font.getStyle()).append(",")
			.append("size:").append(font.getSize()).append(")")
		.append("\n").append("\t").append("backLight").append(":").append(backLight)
		;

		return sb.toString();
	}

	public Font getFont() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
	}

	public boolean isFullscreen() {
		return fullscreen;
	}

	public void setFullscreen(boolean fullscreen) {
		this.fullscreen = fullscreen;
	}

	public boolean isDisplayStatus() {
		return displayStatus;
	}

	public void setDisplayStatus(boolean displayStatus) {
		this.displayStatus = displayStatus;
	}

	public int getColorScheme() {
		return colorScheme;
	}

	public void setColorScheme(int colorScheme) {
		this.colorScheme = colorScheme;
	}

	public boolean isWrapSpaces() {
		return wrapSpaces;
	}

	public void setWrapSpaces(boolean wrapSpaces) {
		this.wrapSpaces = wrapSpaces;
	}

	public boolean isRotated() {
		return rotated;
	}

	public void setRotated(boolean rotated) {
		this.rotated = rotated;
	}

	public boolean isPositionAsPerc() {
		return positionAsPerc;
	}

	public void setPositionAsPerc(boolean positionAsPerc) {
		this.positionAsPerc = positionAsPerc;
	}

	public BackLight getBackLight() {
		return backLight;
	}

	public void setBackLight(BackLight backLight) {
		this.backLight = backLight;
	}

	public int getBookIdx() {
		return bookIdx;
	}

	public void setBookIdx(int bookIdx) {
		this.bookIdx = bookIdx;
	}

	public int getScrollDelay() {
		return scrollDelay;
	}

	public void setScrollDelay(int scrollDelay) {
		this.scrollDelay = scrollDelay;
	}

}
